var ShowData = {
  emp: function(index,emps){             
    //if(index > 0)        
    empList.append('<tr><td colspan="9">&nbsp;</td></tr>');     
    empList.append('<tr><td colspan="9" align="center"><a name="'+emps.id+'"></a>'+emps.type+'</td></tr>');     
    
    $.each(emps.emp,function(i,d){
      var tr = $('<tr>').append('<td>'+(i+1)+'</td>');
      $.each(d.split(',',9),function(j,dd){
        tr.append('<td><span>'+ (dd || '&nbsp;')+'</span></td>');
      });
      empList.append(tr);            
    });
  },
  child : function(index,ch) {         
    var ul = $('<ul>').addClass('menu').attr('id','childId'+index);
    $.each(ch,function(i,d){      
      ul.append('<li><a hideFocus="true" href="#'+d.id+'" ><span>'+d.name+'</span></a></li>');
      if(d.emp) ShowData.emp(i,d);
    });
    return childList.append(ul);
  },
  creatMenu : function() {                  
      $.each(dep,function(i,d){  
        tabs.append($('<li>').attr('tabIndex',i+1).append('<a href="#" hideFocus="true"><span>'+d.name+'</span></a>'));
        ShowData.child(i+1,d.child);
      });
  },
  search: function () {
    var v = searchBox.value;    
    v = v.replace(/([\\\(\)\[\]\+\*\^\$\?\.])/ig,'\\$1');        
    var rows = empList[0].rows || [],len = rows.length;
    if(len == undefined) {rows = [rows];len = 1;}
    var reg = new RegExp(v, "ig");    
    for (var i = 0; i < len; i++) {
      var r = rows[i];
      r.style.display = (v == '' || ((r.innerText || r.textContent).search(reg) > -1))? '' : 'none';
    }
  }
};

$(function(){      
  window.tabs = $('#topTabs');
  window.childList = $('#menuList');
  window.empList = $('#empList');
  window.searchBox = $('#sb_form_q')[0];
  ShowData.creatMenu(); 
  
  var downLists = $('#menuList > ul');
  downLists.hover(function(){
    window.__ishover = true;
  },function(){
    window.__ishover = false;
    downLists.hide();    
  }).find('a').click(function(){
    var hash = document.getElementsByName(this.hash.replace('#',''))[0];
    if(hash) window.scrollTo(0,$(hash.parentNode).offset().top - 170);    
    return false;
  });  
  
  $('#topTabs a').hover(function(){
    window.__ishover = true;
    $('#menuList ul').hide();
    downList = $('#childId'+$(this).parent()[0].tabIndex);    
    var offset = $(this).position(); 
    downList.css('top',offset.top + 25);    
    downList.css('left',offset.left);  
    downList.show();    
  },function(){    
    window.__ishover = false;
    setTimeout(function(){if(!window.__ishover) downLists.hide();},1500); 
  });  
  searchBox.onkeyup = ShowData.search;
});
